package tests.jd;

/*
-------------------------------------------------------------------
BIE is Copyright 2001-2004 Brunswick Corp.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/bie_license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
 */
/*
 * pUpdate.java
 *
 * Created on November 14, 2002, 10:43 AM
 */
import java.sql.*;
import java.io.*;

/**
 *
 * @author  jdepons
 */
public class PUpdate {
    
    static String sep = System.getProperty("line.separator");
    
    /** Creates a new instance of pUpdate */
    public PUpdate() {
    }

    public static void main(String[] args) throws Exception {
        FileOutputStream fos = null;
        Connection conn = null;
        try {
            fos = new FileOutputStream("c:/projectUpdateLog.txt",true);
        }catch (Exception e) {
            e.printStackTrace();
            return;
        }
                
        try {
            int bugId = 0;
            String severity = "";
            String assignTo = "";
            String priority = "";
            String module = "";
            
            if (args.length < 1) {
                System.out.println("usage: java PUpdate -b <bugID> [-option <value>]");
                System.out.println("Options:");
                System.out.println("   -b <bug id>");
                System.out.println("   -s <severity> (Select One,completed,unresolved,High,Medium,Low)");
                System.out.println("   -p <priority> (Select One,High,Medium,Low)");
                System.out.println("   -a <Assign To>");
                System.out.println("   -m <module>");
                return;
            }
            
            for(int i=0;i<args.length;i=i+2) {
                if (args[i].equals("-b")) {
                    bugId = Integer.parseInt(args[i+1]);
                } else if (args[i].equals("-a")) {
                    assignTo = args[i+1];
                } else if (args[i].equals("-s")) {
                    severity = args[i+1];
                } else if (args[i].equals("-p")) {
                    priority = args[i+1];                
                } else if (args[i].equals("-m")) {
                    module = args[i+1];                
                }
            }

            if (bugId==0) {
                throw new Exception("-b flag (bugId) not specified");
            }

            if (!severity.equals("Select One") && !severity.equals("completed") && !severity.equals("unresolved") && !severity.equals("Low") && !severity.equals("High") && !severity.equals("Medium") && !severity.equals("")) {
                throw new Exception("Severity value: " + severity + " is invalid.");
            }
            
            if (!priority.equals("Select One") && !priority.equals("Low") && !priority.equals("High") && !priority.equals("Medium")&& !priority.equals("")) {
                throw new Exception("Priority value: " + priority + " is invalid.");
            }
                        
            
            if (!priority.equals("") || !severity.equals("") || !assignTo.equals("") || !module.equals("")) { 
                System.out.println("You are about to set the following fields for bug #" + bugId);            
                if (!assignTo.equals("")) System.out.println("    Assign To: " + assignTo);
                if (!severity.equals("")) System.out.println("    Severity: " + severity);
                if (!priority.equals("")) System.out.println("    Test Case Priority: " + priority);
                if (!module.equals("")) System.out.println("    Module Being Tested: " + module);

                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String line ="";
                System.out.print(sep + "Would you like to continue with the update? (y/n)");
                while ((line = br.readLine()) != null) {
                    if (line.equals("n")) {
                        throw new Exception("Stopped by user");
                    }else if (line.equals("y")) {
                        break;
                    }else {
                        System.out.print(sep + "Your response was invalid.  Would you like to continue with the update? (y/n)");
                    }
                }                                      
                System.out.println(sep);
            }
                        
            System.out.println(sep);
                        
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://nut/xbotts", "jdepons","bugboy");
            Statement stmt = conn.createStatement();

            //execute updates
            String sql ="";
            
            if (!severity.equals("")) { 
                sql = "update MailForm_entryData set value='" + severity + "' where name='Severity' and MailForm_entryId=" + bugId + " and wobjectId =122";
                fos.write((sql + sep).getBytes());
                stmt.execute(sql);
            }
            
            if (!assignTo.equals("")) {
                sql = "update MailForm_entryData set value='" + assignTo + "' where name='Assign to (if known)' and MailForm_entryId=" + bugId + " and wobjectId =122";
                fos.write((sql + sep).getBytes());
                stmt.execute(sql);
            }
                                                                                
            if (!priority.equals("")) {
                sql = "update MailForm_entryData set value='" + priority + "' where name='Test Case Priority' and MailForm_entryId=" + bugId + " and wobjectId =122";
                fos.write((sql + sep).getBytes());
                stmt.execute(sql);
            }

            if (!module.equals("")) {
                sql = "update MailForm_entryData set value='" + module + "' where name='Program / module being tested:' and MailForm_entryId=" + bugId + " and wobjectId =122";
                fos.write((sql + sep).getBytes());
                stmt.execute(sql);
            }
            
            sql = "select * from MailForm_entryData where MailForm_entryId =" + bugId + " and wobjectID=122";
            fos.write((sql + sep).getBytes());
            ResultSet rs = stmt.executeQuery(sql);

            boolean printHeader = true;        
            while( rs.next() ){
                if (printHeader) {
                    fos.write(("MailForm_entryId = " + rs.getInt("MailForm_entryId") + sep).getBytes());
                    System.out.println("MailForm_entryId = " + rs.getInt("MailForm_entryId") + " ");	
                    fos.write(("wobjectId = " + rs.getInt("wobjectId") + sep + sep).getBytes());
                    System.out.println("wobjectId = " + rs.getInt("wobjectId") + sep);	

                    printHeader = false;
                }
                fos.write((rs.getString("name") + ":" + sep).getBytes());
                System.out.println(rs.getString("name") + ":");	
                fos.write(("      -" + rs.getString("value") + sep).getBytes());
                System.out.println("      -" + rs.getString("value") + " ");	
            }                    
        } catch (Exception e) {
            System.out.println(sep);
            e.printStackTrace(new PrintWriter(fos));
            e.printStackTrace();
        }
        fos.write((sep + "--------" + sep).getBytes());
        fos.flush();
        fos.close();
        if (conn != null) conn.close();
        return;
    }    
}
